package com.cookor.poi.utils;

import com.alibaba.fastjson.JSON;
import com.cookor.poi.common.utils.CacheUtils;
import com.cookor.poi.common.utils.JedisUtils;
import com.google.common.collect.Maps;

import java.util.Map;

public class CacheFactory {
	private static int type = 3;// 1mysql 2Ehcache 3redis

	public CacheFactory() {

	}

	private static int getType() {
		return type;
	}

	public static Object get(String key) {
		if (getType() == 1) {
			return DbCacheUtils.get(key);
		} else if (getType() == 2) {
			return CacheUtils.get(key);
		}else if(getType() == 3) {
			return JedisUtils.getObject(key);
		}
		return null;
	}

	public static void put(String key, Object value) {
		if (getType() == 1) {
			DbCacheUtils.put(key, value);
		} else if (getType() == 2) {
			CacheUtils.put(key, value);
		}else if(getType() == 3) {
			JedisUtils.setObject(key, value, 0);
		}

	}

	/**
	 * 获取缓存
	 *
	 * @param cacheName
	 * @param key
	 * @return
	 */
	public static Object get(String cacheName, String key) {
		if (getType() == 1) {
			return DbCacheUtils.get(cacheName, key);
		} else if (getType() == 2) {
			return CacheUtils.get(cacheName, key);
		}else if(getType()==3){
			return JedisUtils.getObject(cacheName+"_"+key);
		}
		return null;
	}

	/**
	 * 写入缓存
	 *
	 * @param cacheName
	 * @param key
	 * @param value
	 */
	public static void put(String cacheName, String key, Object value) {
		if (getType() == 1) {
			DbCacheUtils.put(cacheName, key, JSON.toJSONString(value));
		} else if (getType() == 2) {
			CacheUtils.put(cacheName, key, value);
		}else if(getType()==3){
			JedisUtils.setObject(cacheName+"_"+key,value,0);
		}
	}

	public static void put(String cacheName, String key, Object value,
                           int nExpireSecond, int nIdleSecond) {
		if (getType() == 1) {
			DbCacheUtils.put(cacheName, key, JSON.toJSONString(value),
					nExpireSecond, nIdleSecond);
		} else if (getType() == 2) {
			Map<String, Object> map = Maps.newConcurrentMap();
			map.put(key, value);
			map.put("nExpireSecond", nExpireSecond);
			map.put("nIdleSecond", nIdleSecond);
			CacheUtils.put(cacheName, key, map);
		}else if (getType() == 3) {
			JedisUtils.setObject(cacheName+"_"+key, value, nExpireSecond);
		}
	}

	/**
	 * 从缓存中移除
	 *
	 * @param cacheName
	 * @param key
	 */
	public static void remove(String cacheName, String key) {
		if (getType() == 1) {
			DbCacheUtils.remove(cacheName, key);
		} else if (getType() == 2) {
			CacheUtils.remove(cacheName, key);
		}else if (getType() == 3) {
			if(JedisUtils.existsObject(cacheName+"_"+key)){
				JedisUtils.delObject(cacheName+"_"+key);
			}else if(JedisUtils.exists(cacheName+"_"+key)){
				JedisUtils.del(cacheName+"_"+key);
			}
		}

	}

	/**
	 * 从缓存中移除
	 *
	 * @param cacheName
	 */
	public static void removeCache(String cacheName) {
		if (getType() == 1) {
			DbCacheUtils.removeCache(cacheName);
		} else if (getType() == 2) {
			CacheUtils.removeAll(cacheName);
		}else if (getType() == 3) {
			if(JedisUtils.existsObject(cacheName)){
				JedisUtils.delObject(cacheName);
			}else if(JedisUtils.exists(cacheName)){
				JedisUtils.del(cacheName);
			}
		}
	}
}
